#pragma once

#include "def.h"
#include <chrono>
#include <sstream>

const char characters[] = "0123456789abcdef";

inline std::string toHex(unsigned char ch)
{
    std::stringstream ss;
    ss << characters[ch / 16] << characters[ch % 16];
    return ss.str();
}

inline std::string getCurrentTime()
{

    std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
    auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
    std::time_t timestamp = tmp.count();

    time_t nowtime;
    struct tm *p;
    time(&nowtime);
    p = localtime(&nowtime);
    char date_buffer[127] = {0};
    sprintf(date_buffer, "%04d-%02d-%02d %02d:%02d:%02d.%d", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, timestamp % 1000);
    std::string current_time(date_buffer);
    return current_time;
}

inline i64 getCurTimeStampMS()
{
    std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
    auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
    return tmp.count();
}

#define LOG() std::cout<<"["<<getCurrentTime()<<"]"



inline bool EQUAL(float x, float y)
{
    return fabsf32(x - y) < 0.0001f;
}

inline void float2Byte(float val, std::vector<u8> &buf)
{
    val = val * 1000;
    u8 low = ((u8)val % 256);
    u8 high = val / 256;

    buf.push_back(high);
    buf.push_back(low);
}

inline void print(const std::vector<u8> &vec, std::string desc = "")
{
    std::string str = "";
    for (auto &val : vec)
    {
        str += toHex(val);
        str += " ";
    }
    std::cout << "[" << desc << "] vec: " << str << std::endl;
}

std::vector<std::string> split(std::string str,std::string s);
u8 checkSum(const std::vector<u8> &buf, int count);